package com.gxa.service.commserve.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.entity.commserve.Repair;
import com.gxa.dto.commservedto.RepairSearchCriteria;
import com.gxa.mapper.commserve.RepairMapper;
import com.gxa.service.commserve.RepairService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:18:00
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {
    @Autowired
    private RepairMapper repairMapper;

    @Override
    public Map<String, Object> queryList(Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashedMap();
        QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
        Page<Repair> page = PageHelper.startPage(pageNum,pageSize);
        List<Repair> repairList = this.repairMapper.selectList(queryWrapper);
        PageInfo<Repair> pageInfo = new PageInfo<>(repairList);
        map.put("pageInfo",pageInfo);
        return map;
    }

    @Override
    public Map<String, Object> queryListBySearch(RepairSearchCriteria repairSearchCriteria, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashedMap();
        QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
        if (repairSearchCriteria.getRepairer()!=null){
            queryWrapper.like("repairer",repairSearchCriteria.getRepairer());
        }
        if (repairSearchCriteria.getContactNumber()!=null){
            queryWrapper.like("contact_number",repairSearchCriteria.getContactNumber());
        }
        if (repairSearchCriteria.getRepairAddress()!=null){
            queryWrapper.like("repair_address",repairSearchCriteria.getRepairAddress());
        }
        if (repairSearchCriteria.getState()!=null){
            queryWrapper.like("state",repairSearchCriteria.getState());
        }
        if (repairSearchCriteria.getStartTime()!=null&&!repairSearchCriteria.getEndTime().equals("")){
            queryWrapper.between("repair_time",repairSearchCriteria.getStartTime(),repairSearchCriteria.getEndTime());
        }
        Page<Repair> page = PageHelper.startPage(pageNum,pageSize);
        List<Repair> repairList = this.repairMapper.selectList(queryWrapper);
        PageInfo<Repair> pageInfo = new PageInfo<>(repairList);
        map.put("pageInfo",pageInfo);
        return map;
    }

    @Override
    public void addRepair(Repair repair) {
        this.repairMapper.insert(repair);
    }
}
